package com.imooc.service.impl;

import com.imooc.mapper.CategoryMapper;
import com.imooc.mapper.CategoryMapperCustom;
import com.imooc.pojo.Category;
import com.imooc.pojo.vo.CategoryVO;
import com.imooc.pojo.vo.NewItemsVO;
import com.imooc.service.CategoryService;
import imooc.utils.JsonUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * @author cg.hu
 * @version 1.0.0
 * @date 2021/1/18 13:55
 * @description
 * @since 1.0.0
 */
@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private CategoryMapperCustom categoryMapperCustom;

    final static Logger logger = LoggerFactory.getLogger(CategoryServiceImpl.class);

    /**
     * @author cg.hu
     * @version 2.6
     * @date 2021/1/18 13:56
     * @description    查询一级分类
     **/
    @Override
    @Transactional(propagation = Propagation.SUPPORTS, rollbackFor = Exception.class)
    public List<Category> queryAllRootLevelCat() throws Exception {
        Example example = new Example(Category.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("type", 1);
        List<Category> categoryList = categoryMapper.selectByExample(example);
        return categoryList;
    }

    /**
     * @author cg.hu
     * @version 2.6
     * @date 2021/1/22 14:17
     * @description    根据一级分类ID，查询其下所有分类
     **/
    @Override
    @Transactional(propagation = Propagation.SUPPORTS, rollbackFor = Exception.class)
    public List<CategoryVO> getSubCatList(Integer rootCatId) throws Exception {
        return categoryMapperCustom.getSubCatList(rootCatId);
    }

    /**
     * @author cg.hu
     * @version 2.6
     * @date 2021/1/22 15:33
     * @description    获取一级分类下的最新六个商品
     **/
    @Override
    @Transactional(propagation = Propagation.SUPPORTS, rollbackFor = Exception.class)
    public List<NewItemsVO> getSixNewItemsLazy(Integer rootCatId) throws Exception {
        Map<String, Object> map = new HashMap<>();
        map.put("rootCatId", rootCatId);
        logger.info("获取一级分类下的最新六个商品,入参: {}", JsonUtils.objectToJson(map));
        return categoryMapperCustom.getSixNewItemsLazy(map);
    }
}
